# gui

from Tkinter import *
import db

# Callback function definition ------------------------------
def ButtonCommand():
    textContent = text.get()
    db.connectToDb()
    rows = db.executeStmt(textContent)
    db.disconnectFromDb()

    rowContent = ""
    for row in rows:
        rowContent = ""
        for col in row:
            rowContent += str(col) + " "
        listbox.insert(END, rowContent)
    
def CleanCommand():
    text.delete(0, END)
    listbox.delete(0, END)
    
# UI definition --------------------------------------------
# instantiates the elements
root = Tk()
root.title("GUI, UI")
topFrame = Frame(root)
bottomFrame = Frame(root)
button = Button(topFrame, text="Get Data", command=ButtonCommand)
buttonClean = Button(topFrame, text="Clean", command=CleanCommand)
text = Entry(topFrame)
scrollbar = Scrollbar(bottomFrame, orient=VERTICAL)
listbox = Listbox(bottomFrame, yscrollcommand=scrollbar.set)
scrollbar.configure(command=listbox.yview)
# connects everything together, position widgets, pack()
text.pack(side=LEFT, fill=X, expand=1)
button.pack(side=LEFT)
buttonClean.pack(side=LEFT)
listbox.pack(side=LEFT, fill=BOTH, expand=1)
scrollbar.pack(side=RIGHT, fill=Y)
topFrame.pack(fill=X)
bottomFrame.pack(fill=BOTH, expand=1)
# sets window size
root.geometry("600x400")

# Run it  ---------------------------------------------------
root.mainloop()
